package cn.hsw.manage.role.service.impl;

import cn.hsw.component.core.enums.HttpEnum;
import cn.hsw.component.core.util.BeanUtil;
import cn.hsw.component.core.util.IdUtil;
import cn.hsw.component.core.util.ResultUtil;
import cn.hsw.component.core.util.Validator;
import cn.hsw.manage.center.entity.RolePermissionCenter;
import cn.hsw.manage.center.entity.UserRoleCenter;
import cn.hsw.manage.center.service.RolePermissionCenterService;
import cn.hsw.manage.center.service.UserRoleCenterService;
import cn.hsw.manage.role.entity.Role;
import cn.hsw.manage.role.mapper.RoleMapper;
import cn.hsw.manage.role.service.RoleService;
import cn.hsw.manage.role.vo.RolePermissionVO;
import cn.hsw.manage.role.vo.RoleVO;
import cn.hutool.core.exceptions.ValidateException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李子木
 * @since 2021-11-03 08:56:11
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    UserRoleCenterService userRoleCenterService;

    @Autowired
    RolePermissionCenterService rolePermissionCenterService;

    @Override
    public Set<Long> getRoleIdSet(Long userPid) {
        return baseMapper.getRoleIdSet(userPid);
    }

    @Override
    public Set<String> getRoleCodeSet(Long userPid) {
        return baseMapper.getRoleCodeSet(userPid);
    }

    @Override
    public void add(RoleVO roleVO) {
        Role roleModel = BeanUtil.copyProperties(roleVO, Role.class);
        roleModel.setCode(IdUtil.generate().toString());
        baseMapper.insert(roleModel);
    }

    @Override
    public void delete(Long pid){
        //判断角色下是否有用户，如果有则禁止删除
        if(userRoleCenterService.validateRoleHaveUser(pid)){
            throw new ValidateException("角色下包含用户,请先是删除用户");
        }
        baseMapper.deleteById(pid);

    }

    @Override
    public void edit(Long pid, RoleVO roleVO) {

        Role roleModel = baseMapper.selectById(pid);

        if(Validator.isNotNull(roleVO.getName())){
            roleModel.setName(roleVO.getName());
        }
        if(Validator.isNotNull(roleVO.getRemark())){
            roleModel.setRemark(roleVO.getRemark());
        }
        this.updateById(roleModel);
    }

    @Override
    public void authorization(RolePermissionVO rolePermissionVO) {
        Role roleModel = baseMapper.selectById(rolePermissionVO.getRolePid());
        Validator.validateNotNull(roleModel,"操作非法,当前角色不存在");

        rolePermissionCenterService.bind(rolePermissionVO.getRolePid(),rolePermissionVO.getPermissionPids());
    }

    @Override
    public RolePermissionVO auth(Long rolePid) {
        List<RolePermissionCenter> rolePermissionCenterList = rolePermissionCenterService.findByRolePid(rolePid);

        List<Long> list = rolePermissionCenterList.stream().map(RolePermissionCenter::getPermissionPid).collect(Collectors.toList());
        RolePermissionVO rolePermissionVO = new RolePermissionVO();
        rolePermissionVO.setRolePid(rolePid);
        rolePermissionVO.setPermissionPids(list);
        return rolePermissionVO;
    }
}
